/* 这里统一配置请求 */
import myAxios from "axios"; 
import {Tools} from "./Tools"

/* 请求基址 */
import {BASE_API_URL} from "../../public/js/baseConfig"

myAxios.defaults.responseType = "json";

/* 请求拦截器 */
myAxios.interceptors.request.use((options)=>{

    /* Token验证 */
    const Token = Tools.verifyToken();
    if(Token){
        options.headers["Token"] = Token;
    }
   
    /* 请求地址拼接 */
    if(!options.url.includes("http")){
        options.url = BASE_API_URL + options.url
    }
    return options;
})

myAxios.interceptors.response.use((options)=>{
    try{
        if(typeof options.data=="string"){options.data = JSON.parse(options.data.trim())}
    }catch(err){
        options.data = {code:'3001',msg:'操作失败!',data:[]};
        throw Error(err)
    }
    /* Token过期未&&登录拦截 */
    if(options.data.code==1000){
        Tools.delToken();
        Tools.showMessage("登录信息已过期,请重新登录!","warning")
        location.href = location.href;
        return;
    }

    return options
})


export default myAxios;